
import UIKit

struct RaeToolWrapper<Base> {
    let base: Base
    init(_ base: Base) {
        self.base = base
    }
}

protocol RaeToolProtocol { }

extension RaeToolProtocol {
    ///
    public var rae: RaeToolWrapper<Self> {
        get { return RaeToolWrapper(self) }
        set { }
    }
    
}

extension UIViewController: RaeToolProtocol { }

extension RaeToolWrapper {
    
    //TODO: - nib
    func nib<T: UIView>(_ view: T.Type, in bundle: Bundle? = Bundle.main) -> T {
        let nibName = view.description().replacingOccurrences(of: "\(targetName()).", with: "")
        if let view = bundle?.loadNibNamed(nibName, owner: nil, options: nil)?.last as? T {
            return view
        }
        return UIView() as! T
    }
    
    //TODO: - 获取控制器
    func controller<T: UIViewController>(_ type: T.Type, in storyboardName: String) -> T {
        let className = type.description().replacingOccurrences(of: "\(targetName()).", with: "")
        let storyBoard = UIStoryboard(name: storyboardName, bundle: Bundle.main)
        guard let controller = storyBoard.instantiateViewController(withIdentifier: className) as? T else {
            return UIViewController() as! T
        }
        return controller
    }
    
    //TODO: - 解析json数据
    func jsonData(_ fileName: String, in bundle: Bundle = Bundle.main) -> Data? {
        guard let path = bundle.path(forResource: fileName, ofType: "json") else { return nil }
        return try? Data(contentsOf: URL(fileURLWithPath: path))
    }
    
    //TODO: - 构建富文本
    func makeAttriString(_ text: String, font: UIFont?, color: UIColor = .white, image: UIImage? = nil, imageSize: CGSize = .zero, imageInFirst: Bool = true) -> NSMutableAttributedString {
        
        let attriString = NSMutableAttributedString(string: text)
        
        let totalCount = attriString.length
        /// 文字颜色
        attriString.addAttribute(.foregroundColor, value: color, range: NSRange(location: 0, length: totalCount))
        /// 字体大小
        if let customFont = font {
            attriString.addAttribute(.font, value: customFont, range: NSRange(location: 0, length: totalCount))
        }
        
        if let img = image {
            let textAttachment = NSTextAttachment()
            textAttachment.image = img
            textAttachment.bounds = CGRect(origin: CGPoint(x: 0, y: -3.5), size: imageSize)
            let imageAttri = NSAttributedString(attachment: textAttachment)
            
            let spaceAttri = NSAttributedString(string: " ")
            
            if imageInFirst {
                attriString.insert(spaceAttri, at: 0)
                attriString.insert(imageAttri, at: 0)
            } else {
                attriString.insert(spaceAttri, at: totalCount)
                attriString.insert(imageAttri, at: attriString.length)
            }
        }
        return attriString
    }
    
    //TODO: - Target name
    func targetName() -> String { ((Bundle.main.infoDictionary?["CFBundleExecutable"] as? String) ?? "") }
    
    //TODO: - display name
    func appName() -> String { ((Bundle.main.infoDictionary?["CFBundleDisplayName"] as? String) ?? "") }
    
    //TODO: - display name
    func appVersion() -> String { ((Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String) ?? "") }
    
    //TODO: - 震动反馈
    func shock(feedback style: UIImpactFeedbackGenerator.FeedbackStyle = .light) {
        // 震动效果
        let gen = UIImpactFeedbackGenerator.init(style: style);//light震动效果的强弱
        gen.prepare();//反馈延迟最小化
        gen.impactOccurred()//触发效果
    }
    
}

